<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>table模块快速使用</title>
    <link rel="stylesheet" href="js/layui/css/layui.css" media="all">
</head>
<body>
<!--表格容器-->
<table id="demo" lay-filter="test"></table>

<!--头部工具栏-->
<script type="text/html" id="toolbarDemo1">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>

    </div>
</script>
<!--行内工具栏-->
<script type="text/html" id="toolbarDemo2">
    <div class="layui-btn-container">

        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</button>
        <button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="update">编辑</button>
    </div>
</script>

<!--添加的表单弹窗-->
<form class="layui-form" action="addForm" style="display: none">
    <div class="layui-form-item">
        <label class="layui-form-label">类型名</label>
        <div class="layui-input-block">
            <input type="text" name="typeName" required  lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>


    <div class="layui-form-item" >
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemoAdd">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<!--修改的表单弹窗-->


<form class="layui-form" action=""  lay-filter="updateFrom" style="display: none">
    <div class="layui-form-item"action="" id="updateFrom"lay-filter="updateFrom"  style="display: none" >
        <label class="layui-form-label">id</label>
        <div class="layui-input-block">
            <input type="text" name="id" autocomplete="off" class="layui-input" readonly>
        </div>
    </div>




    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="fromDemoUpdae">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>






<!--脚本开始-->
<script src="js/layui/layui.js"></script>
<script>
    layui.use(['table','form'] ,function(){
        var table = layui.table;
        var form =layui.form;
        var $=layui.$;

        //监听提交 添加
        form.on('submit(formDemoAdd)', function(data){
            // layer.msg(JSON.stringify(data.field));
            $.post("type/addType",data.field,function (res){
                if(res.code==200){
                    layer.msg("添加成功",{time:200},function (){
                        table.reload("idTest",{});
                        layer.closeAll();
                    })
                }else
                {
                    layer.msg("添加失败")
                }
            },"json")
            return false;
        });
        //监听提交，修改
        form.on('submit(formDemoAdd)', function(data){
            // layer.msg(JSON.stringify(data.field));
            $.post("type/updateType",data.field,function (res){
                if(res.updatecode==200){
                    layer.msg("修改成功",{time:2000},function (){
                        table.reload("idTest",{});
                        layer.closeAll();
                    })
                }else{
                    layui.msg("修改失败")
                }
            },"json")
            return false;
        });

        //触发事件
        //头部工具条触发事件
        table.on('toolbar(test)', function(obj){

            switch(obj.event){
                case 'add':
                    layer.open({
                        type: 1,
                        content: $('#addFrom') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });

            };
        });
        //行内工具触发事件
        table.on('tool(test)', function(obj){

            switch(obj.event){
                case 'delete':
                    layer.msg('删除');
                    break;
                case 'update':
                    // layer.msg('编辑');
                    form.val("UpdateFrom",obj.data)
                    layer.open({
                        type: 1,
                        content: $('#updateFrom',obj.data) //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
            };
        });

        //第一个实例
        table.render({
            elem: '#demo'
            ,id:"idTest"
            ,toolbar: '#toolbarDemo1'
            ,url: 'type/getTypeList' //数据接口
            ,page: true //开启分页
            ,cols: [[ //表头
                {field: 'id', title: 'ID', width:80}
                ,{field: 'typeName', title: '菜品名', width:280}
                ,{ title: '操作', width:280,toolbar: '#toolbarDemo2'}

            ]]
        });

    });
</script>
</body>
</html>